package com.zyd.module.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zyd.common.util.CoreConst;
import com.zyd.common.util.ResultUtil;
import com.zyd.module.admin.model.Role;
import com.zyd.module.admin.vo.base.PageResultVo;
import com.zyd.module.admin.vo.base.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;


import com.zyd.module.admin.model.PictureRoundEntity;
import com.zyd.module.admin.service.PictureRoundService;



/**
 * 
 *
 * @author hui
 * @email *****@mail.com
 * @date 2022-04-04 00:17:38
 */
@Slf4j
@Controller
@RequestMapping("/")
public class PictureRoundController {
    @Autowired
    private PictureRoundService pictureRoundService;


    /**
    * 跳转到页面
    */
    @GetMapping("/index/pictureRound")
    public String pictureRound() {
        return "pictureround/list";
        }


    @PostMapping("pictureRound/list")
    @ResponseBody
    public PageResultVo pageRoles(PictureRoundEntity roundEntity, Integer pageNumber, Integer pageSize) {
        try {
            IPage<PictureRoundEntity> roundEntityIPage = pictureRoundService.selectRounds(roundEntity, pageNumber, pageSize);
            return ResultUtil.table(roundEntityIPage.getRecords(), roundEntityIPage.getTotal());
        } catch (Exception e) {
            log.error(String.format("RoleController.loadRoles%s", e));
            throw e;
        }
    }
    @GetMapping("/round/add")
    public String roundAdd(Model model) {
        PictureRoundEntity roundEntity=new PictureRoundEntity();
        roundEntity.setIsShow(1);
        model.addAttribute("round",roundEntity);
        return CoreConst.ADMIN_PREFIX + "round/form";
    }

    @ApiOperation(value = "新增")
    @PostMapping("/round/save")
    @ResponseBody
    public ResponseVo saveRound(PictureRoundEntity  entity) {
        try {
            boolean a = pictureRoundService.save(entity);
            if (a) {
                return ResultUtil.success("添加数据成功");
            } else {
                return ResultUtil.error("添加数据失败");
            }
        } catch (Exception e) {
            log.error(String.format("RoleController.addRole%s", e));
            throw e;
        }
    }
    @ApiOperation(value = "编辑")
    @GetMapping("/round/edit")
    public String roundEdit(Model model,Integer id) {
        PictureRoundEntity roundEntity=pictureRoundService.getById(id);
        model.addAttribute("round",roundEntity);
        return CoreConst.ADMIN_PREFIX + "round/form";
    }

    @ApiOperation(value = "新增")
    @PostMapping("/round/update")
    @ResponseBody
    public ResponseVo updateRound(PictureRoundEntity  entity) {
        try {
            boolean a = pictureRoundService.updateById(entity);
            if (a) {
                return ResultUtil.success("修改数据成功");
            } else {
                return ResultUtil.error("修改数据失败");
            }
        } catch (Exception e) {
            log.error(String.format("RoleController.addRole%s", e));
            throw e;
        }
    }

    @ApiOperation(value = "删除")
    @PostMapping("/round/delete")
    @ResponseBody
    public ResponseVo deleteRound(Integer id) {
        boolean a = pictureRoundService.removeById(id);
        if (a) {
            return ResultUtil.success("删除数据成功");
        } else {
            return ResultUtil.error("删除数据失败");
        }
    }

    @ApiOperation(value = "删除")
    @PostMapping("/round/batch/delete")
    @ResponseBody
    public ResponseVo deleteBatch(@RequestParam("ids[]") Integer[] ids) {
        boolean a = pictureRoundService.removeByIds(Arrays.asList(ids));
        if (a) {
            return ResultUtil.success("批量删除数据成功");
        } else {
            return ResultUtil.error("批量删除数据失败");
        }
    }

}
